package handler

import (
	"sep16-exam/model"
	"sep16-exam/pkg"

	"github.com/gin-gonic/gin"
)

func Register(c *gin.Context) {
	phone := c.PostForm("phone")
	password := pkg.Md5(c.PostForm("password"))

	user := model.User{
		Phone:    phone,
		Password: password,
	}
	err := model.DB.Create(&user).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	}

	if phone == user.Phone {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "手机号不能重复",
		})
		return
	}

	if password == pkg.Md5(user.Password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不能重复",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
	return
}
